package leetCode.firstWeek;

//class ListNode {
//  int val;
//  ListNode next;
//  ListNode(int x) {
//    val = x;
//  }
//}

public class Code_2 {
  public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode tmp = new ListNode(0); // 新建一个暂存的节点
    ListNode res = tmp; // 结果
    int symbol = 0; // 进位的标志符
    while (l1 != null || l2 != null || symbol != 0) {
      int l1Num = (l1 == null) ? 0 : l1.val;
      int l2Num = (l2 == null) ? 0 : l2.val;
      int carryNum = (l1Num + l2Num + symbol) % 10; // 进位
      symbol = (l1Num + l2Num + symbol) / 10;
      ListNode tmpNode = new ListNode(carryNum);
      if (l1 != null) l1 = l1.next;
      if (l2 != null) l2 = l2.next;
      tmp.next = tmpNode;
      tmp = tmp.next;
    }
    return res.next;
  }
}
